﻿using System;
using System.Data;
using System.Web;
using System.Web.UI.WebControls;
using NCS.Library;
using NCS.Library.Components.Portal;
using NCS.Library.Components.Security;
using NCS.Library.Utilities;

namespace Portal.Administrator.UserControls
{
	public partial class Header : AuthenticatedControl
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			if(!Page.IsPostBack)
			{
				lblCurrentUser.Text = CurrentUser.Username;
			    if(AppEnv.GetLanguage() == "vi-VN")
			    {
			        hplChangePass.Text = "Hồ sơ cá nhân";
                    btnLogout.Text = "Đăng xuất";
                    lblWellcome.Text = "Chào mừng :";
                    //btnGo.ImageUrl = "/administrator/images/go.png";
			        
                    rdoLanguage.Items.Clear();
                    rdoLanguage.Items.Add(new ListItem("Việt Nam", "vi-VN"));
                    rdoLanguage.Items.Add(new ListItem("English", "en-US"));
                    
			    }
			    else
			    {
                    hplChangePass.Text = "Edit profile";
                    btnLogout.Text = "Log Out";
                    lblWellcome.Text = "Wellcome :";
                    //btnGo.ImageUrl = "/administrator/images/go.png";
			        
                    rdoLanguage.Items.Clear();
                    rdoLanguage.Items.Add(new ListItem("Vietnamese", "vi-VN"));
                    rdoLanguage.Items.Add(new ListItem("Tiếng Anh", "en-US"));
			    }

                if (!CurrentUser.IsSuperAdmin && !CurrentUser.IsAdmin)
                {
                    PortalController.LoadPortalsForUserID(dropPortal.Items, CurrentUser.UserID);
                }
                else
                {
                    PortalController.LoadPortals(dropPortal.Items);
                }
			    if(ConvertUtility.ToInt32(AppEnv.PortalId()) !=0)
                    dropPortal.SelectedValue = AppEnv.PortalId();
                hplChangePass.NavigateUrl = "/Administrator/Default.aspx?portalid=" + AppEnv.PortalId() + "&cmd=manageprofile&uid=" + CurrentUser.UserID + "&lang=" + AppEnv.GetLanguage();
			}
		}
		
		protected void rdoLanguage_SelectedIndexChanged(object sender, EventArgs e)
		{
			AppEnv.SetLanguage(rdoLanguage.SelectedValue);
			string curUrl = Request.RawUrl;
			string newUrl = string.Empty;
			if (curUrl.IndexOf("&lang") > -1)
				newUrl = curUrl.Substring(0, curUrl.IndexOf("&lang"));
			else if(curUrl.IndexOf("?lang") > -1)
				newUrl = curUrl.Substring(0, curUrl.IndexOf("?lang"));
			else
				newUrl = curUrl;
            if (!CurrentUser.IsSuperAdmin && !CurrentUser.IsAdmin)
            {
                PortalController.LoadPortalsForUserID(dropPortal.Items, CurrentUser.UserID);
            }
            else
            {
                PortalController.LoadPortals(dropPortal.Items);
            }
            NCS.Library.Components.DataCaching data = new NCS.Library.Components.DataCaching();
            data.RemoveCache("NCS.Portal.Command");
            
			Response.Redirect(newUrl);
            
		}
		protected override void OnPreRender(EventArgs e)
		{
			base.OnPreRender(e);
			rdoLanguage.SelectedValue = AppEnv.GetLanguage();
		}

        protected void btnLogout_Click(object sender, EventArgs e)
        {
            AuthenticateUtility.LogoutUser();
			Response.Redirect("/Administrator/");
        }

        protected void btnGo_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        {
            Response.Redirect(MiscUtility.UpdateQueryStringItem(HttpContext.Current.Request, "portalid", dropPortal.SelectedValue));
        }
	}
}